﻿//********************************************************************************//
//创建日期：<创建日期，2016-06-21>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：非税接口 地表水
//********************************************************************************//
layui.use(['form', 'jquery', 'element', 'laydate', 'upload'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , element = layui.element()
    , laydate = layui.laydate
    , upload = layui.upload;
    /*initial load start*/
    initcontrol();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        var index = layer.load(1);
        load(0);
        //bindOperate(1);
        load(1);
        //bindOperate(2);
        load(2);
        //bindOperate(3);
        clicktrigge(1, 0);
        clicktrigge(2, 0);
        clicktrigge(3, 0);
        layer.close(index);
    }
    /*end initial*/

    /*event*/
    $("#divStatus1 .smenu").click(function () {
        $("#divStatus1 .smenu").each(function () {
            $(this).css("background-color", "");
            $(this).removeAttr("select");
        });
        $(this).css("background-color", "gray");
        $(this).attr("select", "true");
        $("#jdxx1").find("[name='jdbs']").text($(this).attr("vid2"));
        $("table[name='ContentList1i']").each(function () {
            $(this).hide();
        });
        $("#ContentList1_" + $(this).attr("vid")).show();
        loadDetail(1, $("#ContentList1 .datarow").find("td").eq(0).text(), $(this).attr("vid2"), $(this).attr("vid"));
    });
    element.on('tab', function (data) {
        //记录当前选择页索引 
        $("#pstate_Type").text(data.index + 1);
        if (data.index == 0 || data.index == 1) {
            $("#oDR").show();
            $("#oSC").hide();
            if (data.index == 0) {
                $("#oDR").hide();
            }
        }
        else {
            $("#oDR").hide();
            $("#oSC").show();
        }
        //加载文件列表
        //load(data.index);
    });
    $("#oSX").click(function () {
        var index = layer.load(1);
        load(0);
        load(1);
        load(2);
        clicktrigge(1, 0);
        clicktrigge(2, 0);
        clicktrigge(3, 0);
        layer.close(index);
    });
    function load(type) {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/QSSFZW/FeiShuiJK.ashx",
            data: {
                "op": "WJXX_HQ",
                "type": type
            },
            dataType: "json",
            success: function (rs) {
                var no = type + 1;
                $("#ContentList" + no + " .datarow").remove();
                var strs = "";
                for (var i = 0; i < rs.length; i++) {
                    var vids = new Array();//记录完整信息,以便选中显示在右侧
                    //debugger;
                    vids.push(rs[i]["S_FILENAME"]);
                    vids.push(rs[i]["D_CREATEDATE"]["DateString"]);
                    vids.push(rs[i]["S_PICI"]);
                    vids.push(rs[i]["S_DANWEIBH"]);
                    vids.push(rs[i]["S_BUMENBH"]);
                    vids.push(rs[i]["I_JIEDUANS"])
                    trstr = "<tr class='datarow' style='cursor:pointer;' filename='" + rs[i]["S_FILENAME"] + "' vid='" + vids.join("&") + "'><td>" + rs[i]["S_FILENAME"] + "</td><td>" + rs[i]["D_CREATEDATE"]["DateString"] + "</td><td>" + rs[i]["I_JIEDUANS"] + "</td></tr>";
                    strs = strs + trstr;
                }
                $("#ContentList" + no).append(strs);
                bindOperate(no);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }
    function bindOperate(no) {
        //添加点击事件
        $("#ContentList" + no).find(".datarow").click(function () {
            //清除所有项选中状态
            $("#ContentList" + no).find(".datarow").each(function () {
                $(this).removeAttr("select");
                $(this).css("background-color", "");
            });
            //设置当前项选中状态
            $(this).attr("select", "select");
            $(this).css("background-color", "yellowgreen");
            //加载文件信息
            var infos = $(this).attr("vid").split("&");
            $("#wjxx" + no).find("[name='wjm']").text(infos[0]);
            $("#wjxx" + no).find("[name='csrq']").text(infos[1]);
            $("#wjxx" + no).find("[name='pc']").text(infos[2]);
            $("#wjxx" + no).find("[name='dw']").text(infos[3]);
            $("#wjxx" + no).find("[name='bm']").text(infos[4]);
            $("#wjxx" + no).find("[name='jd']").text(infos[5]);
            //加载节段代码
            var jdbs = $("#divStatus" + no).find(".smenu[select]").attr("vid2");
            var order = $("#divStatus" + no).find(".smenu[select]").attr("vid");
            $("#jdxx" + no).find("[name='jdbs']").text(jdbs);
            //加载明细信息(含统计右侧节段信息)
            var index = layer.load(1);
            loadDetail(no, $(this).find("td").eq(0).text(), jdbs, order);
            layer.close(index);
        });
    }
    function loadDetail(no, wjm, jdbs, order) {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/QSSFZW/FeiShuiJK.ashx",
            data: {
                "op": "MXXX_HQ",
                "no": no,
                "wjm": wjm,
                "jdbs": jdbs,
                "order": order
            },
            dataType: "json",
            success: function (rs) {
                //加载数据行
                $("#ContentList" + no + "_" + order + " .datarow").remove();
                var strs = "";
                var trstr = "";
                if (no == 1) {
                    trstr = (order == 1) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>" :
                          (order == 2) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td></tr>" :
                          (order == 3) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>" :
                          (order == 4) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr>" :
                          (order == 5) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr>" :
                          (order == 6) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td></tr>" :
                          (order == 7) ? "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>" :
                          ""
                    ;
                }
                else if (no == 2 || no == 3) {
                    trstr = "<tr class='datarow' style='cursor:pointer;'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr>";
                }
                var trstrtemp = "";
                for (var i = 0; i < rs.length; i++) {
                    if (no == 1) {
                        trstrtemp = (order == 1) ? trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SNAME"]).replace("{2}", rs[i]["SITEMID"]).replace("{3}", rs[i]["SSHORTID"])
                              : (order == 2) ? trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SNAME"])
                              : (order == 3) ? trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SSHORTID"]).replace("{2}", rs[i]["SACCOUNTNO"]).replace("{3}", rs[i]["SACCOUNTNAME"])
                              : (order == 4) ? trstr.replace("{0}", rs[i]["SITEMID"]).replace("{1}", rs[i]["SNAME"]).replace("{2}", rs[i]["SZJLX"]).replace("{3}", rs[i]["NPAYUP"]["ValueString"]).replace("{4}", rs[i]["NPAYDOWN"]["ValueString"]).replace("{5}", rs[i]["SMETRIC"]).replace("{6}", rs[i]["IPAYTIME"]["ValueString"]).replace("{7}", rs[i]["SSHORTID"]).replace("{8}", rs[i]["SBANKCODE"]).replace("{9}", rs[i]["SACCOUNTNO"])
                              : (order == 5) ? trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SPJLX"]).replace("{2}", rs[i]["SNAME"]).replace("{3}", rs[i]["SSHORTNAME"]).replace("{4}", rs[i]["SCXBZ"]).replace("{5}", rs[i]["INUMBER"]["ValueString"]).replace("{6}", rs[i]["IPAGENUM"]["ValueString"]).replace("{7}", rs[i]["NMONEY"]["ValueString"]).replace("{8}", rs[i]["DTREGDATE"]["DateString"]).replace("{9}", rs[i]["DTCANCELDATE"]["DateString"])
                              : (order == 6) ? trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SITEMID"]).replace("{2}", rs[i]["SSHORTID"]).replace("{3}", rs[i]["DTREGDATE"]["DateString"]).replace("{4}", rs[i]["SCXBZ"]).replace("{5}", rs[i]["DTCANCELDATE"]["DateString"]).replace("{6}", rs[i]["SCANCELFILENO"]).replace("{7}", rs[i]["SCANCELREMARK"]).replace("{8}", rs[i]["SMEMO"])
                              : (order == 7) ? trstr.replace("{0}", rs[i]["SITEMID"]).replace("{1}", rs[i]["SID"]).replace("{2}", rs[i]["DTREGDATE"]["DateString"]).replace("{3}", rs[i]["SCXBZ"]).replace("{4}", rs[i]["DTCANCELDATE"]["DateString"]).replace("{5}", rs[i]["SCANCELFILENO"]).replace("{6}", rs[i]["SCANCELREMARK"]).replace("{7}", rs[i]["SMEMO"])
                              : ""
                        ;
                    }
                    else if (no == 2) {
                        trstrtemp = trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SBEGINID"]).replace("{2}", rs[i]["SBANKCODE"]).replace("{3}", rs[i]["NMONEY"]["ValueString"]).replace("{4}", rs[i]["NLATEFEE"]["ValueString"]).replace("{5}", rs[i]["DTREGDATE"]["DateString"]).replace("{6}", rs[i]["SCASHIER"]).replace("{7}", rs[i]["DTCHECKBILL"]["DateString"]);
                    }
                    else if (no == 3) {
                        trstrtemp = trstr.replace("{0}", rs[i]["SID"]).replace("{1}", rs[i]["SBEGINID"]).replace("{2}", rs[i]["STYPE"]).replace("{3}", rs[i]["SENDID"]).replace("{4}", rs[i]["SITEMID"]).replace("{5}", rs[i]["INUMBER"]["ValueString"]).replace("{6}", rs[i]["NPRICE"]["ValueString"]).replace("{7}", rs[i]["NMONEY"]["ValueString"]);
                    }
                    strs = strs + trstrtemp;
                }
                $("#ContentList" + no + "_" + order).append(strs);
                //加载数据行信息
                $("#jdxx" + no).find("[name='count']").text(rs.length);
                if (no == 2 || no == 3) {
                    //加载总金额
                    var jesum = 0;
                    $("#ContentList" + no + "_" + order + " .datarow").each(function () {
                        if (no == 2) {
                            jesum = jesum + parseFloat($(this).find("td").eq(3).text());
                        }
                        else {
                            jesum = jesum + parseFloat($(this).find("td").eq(7).text());
                        }
                    });
                    $("#jdxx" + no).find("[name='money']").text(jesum.toFixed(2));
                }
                //加载集中汇缴书数目
                if (no == 3) {
                    $("#jdxx" + no).find("[name='count2']").text(rs.length);
                }
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }
    function clicktrigge(pageno, rowindex) {
        if ($("#ContentList" + pageno).find(".datarow").length > 0) {
            $("#ContentList" + pageno).find(".datarow").eq(rowindex).click();
        }
    }
    upload({
        elem: '#ofDR',
        url: '/ashx/QSSFZW/FeiShuiJK.ashx?op=DR',
        ext: 'txt',
        method: 'post',
        before: function (input) {

        },
        success: function (res, input) {
            if (res.State) {
                layer.alert(res.Msg, function (index) {
                    var indext = layer.load(1);
                    var pageno = $("#pstate_Type").text();
                    load(pageno);
                    $("#ContentList" + pageno + " .datarow").each(function () {
                        $(this).css("background-color", "");
                    });
                    $("#ContentList" + pageno + " .datarow[vid2='" + res.Data1 + "']").css("background-color", "yellowgreen");
                    if ($("#ContentList" + pageno).find(".datarow").length > 0) {
                        $("#ContentList" + pageno).find(".datarow").eq(0).click();
                    }
                    layer.close(indext);
                    layer.close(index);
                });
            }
            else {
                layer.alert(res.Msg);
            }
        }
    });

    $("#oTQ").click(function () {
        var pageno = $("#pstate_Type").text();
        if ($("#ContentList" + pageno + " .datarow[select]").length == 0) {
            layer.msg("请选择要导出的项");
            return;
        }
        var filename = $("#ContentList" + pageno + " .datarow[select]").attr("filename");
        var type = pageno - 1;
        var url = "/ashx/QSSFZW/FeiShuiJK.ashx?op=TQ&type=" + type + "&filename=" + filename;
        window.open(url);
        //$("iframe").remove();
        // var iframe = "<iframe style='display:none' src='" + url + "'></iframe>"
        // $("body").append(iframe);
    });
    $("#oQX").click(function () {
        var pageno = $("#pstate_Type").text();
        if ($("#ContentList" + pageno + " .datarow[select]").length == 0) {
            layer.msg("请选择要取消的项");
            return;
        }
        layer.confirm("确定要取消吗?", function () {
            var filename = $("#ContentList" + pageno + " .datarow[select]").attr("filename");
            var type = pageno - 1;
            $.ajax({
                type: "post",
                cache: false,
                async: false,
                url: "/ashx/QSSFZW/FeiShuiJK.ashx",
                data: {
                    "op": "QX",
                    "type": type,
                    "filename": filename
                },
                dataType: "json",
                success: function (rs) {
                    layer.msg(rs.Msg);
                    if (rs.State) {
                        load(type);
                        clicktrigge(pageno, 0);
                    }
                },
                error: function (error) {
                    layer.msg(error.responseText);
                }
            });
        });
    });
    $("#oSC").click(function () {
        var url = "/ashx/QSSFZW/FeiShuiJK.ashx?op=SC";
        window.open(url);
        //$.ajax({
        //    type: "post",
        //    cache: false,
        //    async: false,
        //    url: "/ashx/QSSFZW/FeiShuiJK.ashx",
        //    data: {
        //        "op": "SC"
        //    },
        //    dataType: "json",
        //    success: function (rs) {
        //        if (rs.State != undefined && rs.State != 'undefined') {
        //            layer.msg(rs.Msg);
        //        }
        //    },
        //    error: function (error) {
        //        layer.msg(error.responseText);
        //    }
        //});
    });
    /*event end*/

    function verify() {
        return true;
    }

    /*logic end*/
});
//layui.use('upload', function () {

//});